

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>The borg module &mdash; pyamp v1.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="top" title="pyamp v1.2 documentation" href="../index.html" />
    <link rel="up" title="The patterns module" href="mod_pyamp_patterns.html" />
    <link rel="next" title="The decorators module" href="pyamp_patterns_decorators.html" />
    <link rel="prev" title="The interface module" href="pyamp_patterns_interfaces_interface.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="pyamp_patterns_decorators.html" title="The decorators module"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="pyamp_patterns_interfaces_interface.html" title="The interface module"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="mod_pyamp.html" >The pyamp module</a> &raquo;</li>
          <li><a href="mod_pyamp_patterns.html" accesskey="U">The patterns module</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-pyamp.patterns.borg">
<span id="the-borg-module"></span><h1>The borg module<a class="headerlink" href="#module-pyamp.patterns.borg" title="Permalink to this headline">¶</a></h1>
<p>The borg module contains the Borg class which provides an implementation
of the Borg design pattern, which is a Singleton like pattern for Python.</p>
<div class="section" id="the-borg-class">
<h2>The Borg class<a class="headerlink" href="#the-borg-class" title="Permalink to this headline">¶</a></h2>
<p class="graphviz">
<img src="../_images/inheritance-a339bc84c3b83f968bdb3c5284d57dff27ce83a4.png" alt="Inheritance diagram of pyamp.patterns.borg.Borg" usemap="#inheritance96a6395ce9" class="inheritance"/>
<map id="inheritance96a6395ce9" name="inheritance96a6395ce9">
<area shape="rect" href="#pyamp.patterns.borg.Borg" title="pyamp.patterns.borg.Borg" alt="" coords="5,7,195,33"/>
</map>
</p>
<dl class="class">
<dt id="pyamp.patterns.borg.Borg">
<em class="property">class </em><tt class="descclassname">pyamp.patterns.borg.</tt><tt class="descname">Borg</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="reference internal" href="../_modules/pyamp/patterns/borg.html#Borg"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pyamp.patterns.borg.Borg" title="Permalink to this definition">¶</a></dt>
<dd><p>The Borg class implements the Borg design pattern which provides
a singleton like pattern for Python.</p>
<p>A Borg object can be accessed by calling the <a class="reference internal" href="#pyamp.patterns.borg.Borg.getInstance" title="pyamp.patterns.borg.Borg.getInstance"><tt class="xref py py-func docutils literal"><span class="pre">getInstance()</span></tt></a> function.
This functions returns an instance of the Borg class which stores its state
between successive calls to get the Borg object.</p>
<p>Instances of the Borg class can implement the <a class="reference internal" href="#pyamp.patterns.borg.Borg.init" title="pyamp.patterns.borg.Borg.init"><tt class="xref py py-func docutils literal"><span class="pre">init()</span></tt></a> function which
is called when the Borg class is first created and initialized. This allows
the concrete Borg classes to set up the initial state for their object.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Example</span><span class="p">(</span><span class="n">Borg</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">init</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;x&#39;</span><span class="p">]</span>

<span class="k">class</span> <span class="nc">First</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># An example of passing a value to the Borg instance</span>
        <span class="n">b</span> <span class="o">=</span> <span class="n">Example</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span>
        <span class="k">print</span> <span class="s">&quot;First, b.x:&quot;</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">x</span>

<span class="k">class</span> <span class="nc">Second</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># Optionally could use Borg.getInstance()</span>
        <span class="n">b</span> <span class="o">=</span> <span class="n">Example</span><span class="o">.</span><span class="n">getInstance</span><span class="p">()</span>
        <span class="k">print</span> <span class="s">&quot;Second, b.x:&quot;</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">x</span>
        <span class="n">b</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="mi">500</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">First</span><span class="p">()</span>
    <span class="n">Second</span><span class="p">()</span>

    <span class="n">b</span> <span class="o">=</span> <span class="n">Example</span><span class="o">.</span><span class="n">getInstance</span><span class="p">()</span>
    <span class="k">print</span> <span class="s">&quot;Third, b.x:&quot;</span><span class="p">,</span> <span class="n">b</span><span class="o">.</span><span class="n">x</span>

    <span class="c"># Prints:</span>
    <span class="c">#    First, b.x: 200</span>
    <span class="c">#    Second, b.x: 200</span>
    <span class="c">#    Third, b.x: 500</span>
</pre></div>
</div>
<ul class="simple">
<li>args &#8211; The arguments</li>
<li>kwargs &#8211; The keyword arguments</li>
</ul>
<dl class="classmethod">
<dt id="pyamp.patterns.borg.Borg.getInstance">
<em class="property">classmethod </em><tt class="descname">getInstance</tt><big>(</big><big>)</big><a class="reference internal" href="../_modules/pyamp/patterns/borg.html#Borg.getInstance"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pyamp.patterns.borg.Borg.getInstance" title="Permalink to this definition">¶</a></dt>
<dd><p>Get an instance of the Borg object.</p>
</dd></dl>

<dl class="method">
<dt id="pyamp.patterns.borg.Borg.getPrivateAttr">
<tt class="descname">getPrivateAttr</tt><big>(</big><em>name</em>, <em>default=None</em><big>)</big><a class="reference internal" href="../_modules/pyamp/patterns/borg.html#Borg.getPrivateAttr"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pyamp.patterns.borg.Borg.getPrivateAttr" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the value of a private class attribute.</p>
<ul class="simple">
<li>name &#8211; The name of the private attribute</li>
<li>default &#8211; The value returned if the attribute does not exist</li>
</ul>
</dd></dl>

<dl class="method">
<dt id="pyamp.patterns.borg.Borg.init">
<tt class="descname">init</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="reference internal" href="../_modules/pyamp/patterns/borg.html#Borg.init"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pyamp.patterns.borg.Borg.init" title="Permalink to this definition">¶</a></dt>
<dd><p>This function is called the first time the class is initialized,
and should be overridden by concrete subclasses.</p>
<ul class="simple">
<li>args &#8211; The arguments</li>
<li>kwargs &#8211; The keyword arguments</li>
</ul>
</dd></dl>

<dl class="method">
<dt id="pyamp.patterns.borg.Borg.setPrivateAttr">
<tt class="descname">setPrivateAttr</tt><big>(</big><em>name</em>, <em>value</em><big>)</big><a class="reference internal" href="../_modules/pyamp/patterns/borg.html#Borg.setPrivateAttr"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pyamp.patterns.borg.Borg.setPrivateAttr" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the value of a private class attribute.</p>
<ul class="simple">
<li>name &#8211; The name of the private attribute</li>
<li>value &#8211; The value to set the attribute to</li>
</ul>
</dd></dl>

</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">The borg module</a><ul>
<li><a class="reference internal" href="#the-borg-class">The Borg class</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="pyamp_patterns_interfaces_interface.html"
                        title="previous chapter">The interface module</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="pyamp_patterns_decorators.html"
                        title="next chapter">The decorators module</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/_generated/pyamp_patterns_borg.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="pyamp_patterns_decorators.html" title="The decorators module"
             >next</a> |</li>
        <li class="right" >
          <a href="pyamp_patterns_interfaces_interface.html" title="The interface module"
             >previous</a> |</li>
        <li><a href="../index.html">pyamp v1.2 documentation</a> &raquo;</li>
          <li><a href="mod_pyamp.html" >The pyamp module</a> &raquo;</li>
          <li><a href="mod_pyamp_patterns.html" >The patterns module</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Brett Ponsler.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.1.
    </div>
  </body>
</html>